package com.wxtl.ws;

import java.io.IOException;
import java.io.PrintWriter;
import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.struts2.ServletActionContext;

import com.opensymphony.xwork2.ActionContext;
import com.wxtl.entity.Customer;
import com.wxtl.entity.UpgradePack;
import com.wxtl.exception.ServiceException;
import com.wxtl.service.CustomerService;
import com.wxtl.service.UpgradePackService;
import com.wxtl.util.JaxbUtil;
import com.wxtl.util.UPSProtocol;
import com.wxtl.ws.qo.UpgradePackQo;
import com.wxtl.ws.vo.Data;
import com.wxtl.ws.vo.RegisterResult;
import com.wxtl.ws.vo.UPResult;

/**
 * WS服务
 * @author hc24
 *
 */
public class UpPackAction extends UPSProtocol{
	private UpgradePackQo packQo;
	private int upId;
	private String dosn;

	private UpgradePack pack;

	private UpgradePackService upgradePackService;
	private CustomerService customerService;



	/**
	 * 客户获取升级列表
	 * 
	 * @return
	 * @throws Exception
	 */
	public String queryPackList(){
		System.out.println("UpPackAction.java:捕获到请求....");
		packQo = new UpgradePackQo();
		createPackQo();
		// 可以做些参数验证
		//请求列表的时候 不判断
		/*if (packQo == null)
			throw new ServiceException("参数为空");
		if (packQo.getAuthsn() == null || "".equals(packQo.getAuthsn().trim()))
			throw new ServiceException("授权编号不能为空");
		if (packQo.getCustsn() == null || "".equals(packQo.getCustsn().trim()))
			throw new ServiceException("客户编号不能为空");
		if(packQo.getProlist()==null||"".equals(packQo.getProlist().trim()))
			throw new ServiceException("产品列表不能为空");			
		if (packQo.getEntname() == null
				|| "".equals(packQo.getEntname().trim()))
			throw new ServiceException("企业名称不能为空");*/

		ActionContext actionContext = ActionContext.getContext();
		HttpServletRequest request = (HttpServletRequest) actionContext
				.get(ServletActionContext.HTTP_REQUEST);
		System.out.println("UpPackAction.java:request.getRemoteHost():"+request.getRemoteHost());
		System.out.println("UpPackAction.java:request.getContentType():"+request.getContentType());
		HttpServletResponse response = (HttpServletResponse) actionContext
				.get(ServletActionContext.HTTP_RESPONSE);

		PrintWriter out = null;
		response.setCharacterEncoding("utf-8");
		response.setContentType("application/xml;charset=UTF-8");
		try {
			out = response.getWriter();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		List<UpgradePack> list = upgradePackService.queryPackList(packQo);
		UPResult upResult = new UPResult();
		upResult.setUplist(list);
		upResult.setSuccess(true);
		//upResult.getViewurl()+"upId=";


		String strResult = JaxbUtil.marshal(upResult, UPResult.class,
				UpgradePack.class);
		strResult=replace(strResult);
		
		out.write(strResult);
		out.flush();
		out.close();

		return null;
	}
	public void createPackQo(){
		packQo.setAuthsn(this.getAuthsn());
		packQo.setCustsn(this.getCustsn());
		packQo.setEntname(this.getEntname());
		packQo.setPluglist(this.getPluglistl());
		packQo.setProlist(this.getProlist());
		packQo.setOther(this.getOther());
		
	}
	
	/**
	 * 获取升级详情
	 * @return
	 * @throws Exception 
	 */
	public String queryPackDetailXml() throws Exception{
		if(this.upId==0)
			throw new ServiceException("ID不能为空");
		
		ActionContext actionContext = ActionContext.getContext();

		HttpServletRequest request = (HttpServletRequest) actionContext
				.get(ServletActionContext.HTTP_REQUEST);
		HttpServletResponse response = (HttpServletResponse) actionContext
				.get(ServletActionContext.HTTP_RESPONSE);
		PrintWriter out = null;

		response.setCharacterEncoding("utf-8");
		response.setContentType("application/xml;charset=UTF-8");
		out = response.getWriter();

		UpgradePack upPack = upgradePackService.getPackById(this.getUpId());

		String strResult = JaxbUtil.marshal(upPack, UpgradePack.class);
		strResult=replace(strResult);
		
		out.write(strResult);
		out.flush();
		out.close();

		return null;
	}
	
	/**
	 * 获取升级详情
	 * @return
	 * @throws Exception 
	 */
	public String queryPackDetail() throws Exception{
		if(this.upId==0)
			throw new ServiceException("ID不能为空");

		UpgradePack upPack = upgradePackService.getPackById(this.getUpId());
		String ver=upPack.getSuptVersion();
		String rev=upPack.getSuptRevision();
		String[] strVers=ver.split(",");
		String[] strRevs=rev.split(",");
		if(strVers.length>1){
			if(strVers[1].equals("+")){
				upPack.setSuptVersion(strVers[0]+"以上");
			}else if(strVers[1].equals("-")){
				upPack.setSuptVersion(strVers[0]+"以下");
			}else{
				upPack.setSuptVersion(strVers[0]+"至"+strVers[1]);
			}
		}
		
		if(strRevs.length>1){
			if(strRevs[1].equals("+")){
				upPack.setSuptRevision(strRevs[0]+"以上");
			}else if(strRevs[1].equals("-")){
				upPack.setSuptRevision(strRevs[0]+"以下");
			}else{
				upPack.setSuptRevision(strRevs[0]+"至"+strRevs[1]);
			}
		}
		
		
		this.pack=upPack;

		return "success";
	}
	
	/**
	 * 用户安装升级包成功后向平台报告数据
	 * @return
	 * @throws Exception 
	 */
	public String reportLog() throws Exception{
		if(this.getUpgradeId()==null||"".equals(this.getUpgradeId().trim()))
			throw new ServiceException("upgradeid不能为空");
		if(this.getCustsn()==null||"".equals(this.getCustsn()))
			throw new ServiceException("custsn不能为空");
//		if(this.getUpCode()==null||"".equals(this.getUpCode()))
//			throw new ServiceException("upcode不能为空");
//		if(this.getVersion()==null||"".equals(this.getVersion()))
//			throw new ServiceException("version不能为空");
//		if(this.getRevision()==null||"".equals(this.getRevision()))
//			throw new ServiceException("revision不能为空");
		upgradePackService.reportLog(this);//记录客户安装情况
		
		//没有抛出异常证明更新成功
		Data data=new Data();
		data.setMsg("数据报告成功");
		data.setSuccess(true);
		
		ActionContext actionContext = ActionContext.getContext();

		HttpServletRequest request = (HttpServletRequest) actionContext
				.get(ServletActionContext.HTTP_REQUEST);
		HttpServletResponse response = (HttpServletResponse) actionContext
				.get(ServletActionContext.HTTP_RESPONSE);
		PrintWriter out = null;

		response.setCharacterEncoding("utf-8");
		response.setContentType("application/xml;charset=UTF-8");
		out = response.getWriter();

		String strResult = JaxbUtil.marshal(data, Data.class);
		strResult=replace(strResult);
		
		out.write(strResult);
		out.flush();
		out.close();

		return null;
	}
	
	
	
	/**
	 * 注册
	 * @return
	 * @throws Exception
	 */
	public String register() throws Exception{
		//数据验证
		if(this.getDogsn()==null||"".equals(this.getDogsn()))
			throw new ServiceException("加密狗编号不能为空");
		if(this.getEntname()==null||"".equals(this.getEntname()))
			throw new ServiceException("企业名称不能为空");
		if(this.getProvince()==null||"".equals(this.getProvince().trim())||this.getProvince().trim().length()<2)
			throw new ServiceException("省份不能为空");
		if(this.getCity()==null||"".equals(this.getCity().trim())||this.getCity().trim().length()<2)
			throw new ServiceException("城市不能为空");
		
		//注册
		Customer customer= customerService.register(this);
		
		RegisterResult rr=new RegisterResult();
		rr.setSuccess(true);
		rr.setMsg("注册成功");
		rr.setAuthsn(customer.getAuthsn());
		rr.setCustsn(customer.getCustsn());
		
		ActionContext actionContext = ActionContext.getContext();

		HttpServletRequest request = (HttpServletRequest) actionContext
				.get(ServletActionContext.HTTP_REQUEST);
		HttpServletResponse response = (HttpServletResponse) actionContext
				.get(ServletActionContext.HTTP_RESPONSE);
		PrintWriter out = null;
		//System.out.println(request.getCharacterEncoding());
		response.setCharacterEncoding("utf-8");
		response.setContentType("application/xml;charset=UTF-8");
		out = response.getWriter();

		String strResult = JaxbUtil.marshal(rr, RegisterResult.class);
		strResult=replace(strResult);
		
		out.print(strResult);
		out.flush();
		out.close();
		return null;
	}
	
	private String replace(String str){
		return str.replaceAll(" xsi:nil=\"true\" xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\"", "");
	}
	
	public void setUpgradePackService(UpgradePackService upgradePackService) {
		this.upgradePackService = upgradePackService;
	}

	public void setCustomerService(CustomerService customerService) {
		this.customerService = customerService;
	}

	public void setPackQo(UpgradePackQo packQo) {
		this.packQo = packQo;
	}

	public UpgradePackQo getPackQo() {
		return packQo;
	}

	public int getUpId() {
		return upId;
	}

	public void setUpId(int upId) {
		this.upId = upId;
	}

	public String getDosn() {
		return dosn;
	}

	public void setDosn(String dosn) {
		this.dosn = dosn;
	}

	public UpgradePack getPack() {
		return pack;
	}

	public void setPack(UpgradePack pack) {
		this.pack = pack;
	}	
}
